package com.example.spinnertest;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.spinnertest.tools.SpinnerPopWindow;
import com.example.spinnertest.tools.SpinnerPopWindow.IOnItemSelectListener;
import com.example.spinnertest.tools.ViewGroupTest;

public class MainActivity extends Activity  implements OnClickListener{

	public  String province = "山东";
	RelativeLayout reLayout = null;
	TextView proTv = null;
	ImageView proImg = null;
	SpinnerPopWindow spinnerCountry = null;
	String []country;
	IOnItemSelectListener itemSelectListenr;
	ViewGroupTest viewGroup ;
	View tvViewGroupItem ,reViewGroupItem;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView( R.layout.activity_main);
	    
		viewGroup = (ViewGroupTest) findViewById(R.id.myviewgroup);
		
		//获取“country”和RelativiLayout控件
		reViewGroupItem =  LayoutInflater.from(this).inflate(R.layout.viewgroup_item_relativelayout, null);
		
		viewGroup.addView(reViewGroupItem);
		
		initial();
	}

	
	public void initial()
	{
		reLayout = (RelativeLayout) findViewById(R.id.spinnerRelativeLayout);
		proTv = (TextView) findViewById(R.id.provinceTextView);
		proTv.setText(province);
		setUpCountry();
	}

	public void setUpCountry()
	{
		if(true)
		{
			System.out.println(">>>>>>>>>>>>>>>>>>>");
		}
		reLayout.setOnClickListener(this);
	country = getResources().getStringArray(R.array.spinner_country);
//		spinnerCountry = new SpinnerPopWindow(this);
//		spinnerCountry.setSpinnerAdapter(country);
//		//
//		spinnerCountry.setItemSelectListener(new IOnItemSelectListener() {
//			
//			@Override
//			public void onItemClick(int position) {
//				// TODO Auto-generated method stub
//				setCountry(position);
//			}
//		});
		
	}
	
	//设置显示样式
	public void setCountry(int position)
	{
		proTv.setText(country[position]);
		province = country[position];
	}
	
	//设置reLayout处下拉
	public void showCountry()
	{
		spinnerCountry.setWidth(reLayout.getWidth());
		spinnerCountry.showAsDropDown(reLayout);
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.spinnerRelativeLayout:
			//必须用当前交互界面的上下文Mainactivity.class而不是getApplicationContext()
			new AlertDialog.Builder(MainActivity.this).setItems(R.array.spinner_country, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					setCountry(which);
				}
			}).show();
			//showCountry();
			break;

		default:
			break;
		}
		
	}
	

}
